
package com.wobaby.util.xml;

import org.w3c.dom.*;

public interface DocumentNodeReader
{
    public static class Support
    {

        public static void readNode(DocumentNodeReader nodereader, Node node)
        {
            readAttributes(nodereader, node);
            readChildNodes(nodereader, node);
        }

        public static void readAttributes(DocumentNodeReader nodereader, Node node)
        {
            NamedNodeMap attributes = node.getAttributes();
            for(int index = 0; index < attributes.getLength(); index++)
            {
                Node attribute = attributes.item(index);
                nodereader.readAttribute(attribute.getNodeName(), attribute.getNodeValue(), attribute);
            }

        }

        public static void readChildNodes(DocumentNodeReader nodereader, Node node)
        {
            NodeList childnodes = node.getChildNodes();
            for(int index = 0; index < childnodes.getLength(); index++)
            {
                Node childnode = childnodes.item(index);
                nodereader.readChildNode(childnode.getNodeName(), childnode.getNodeType(), childnode);
            }

        }

        public static String readChildTextNodesIntoString(Node node, boolean trimwhite)
        {
            NodeList childnodes = node.getChildNodes();
            String result = "";
            boolean textnode = true;
            for(int index = 0; textnode && index < childnodes.getLength(); index++)
            {
                Node childnode = childnodes.item(index);
                if(childnode.getNodeType() == 3)
                {
                    if(trimwhite)
                        result = readChildTextNodeAndConcatenateString(result, childnode);
                    else
                        result = result + childnode.getNodeValue();
                } else
                {
                    textnode = false;
                }
            }

            return result;
        }

        public static String readChildTextNodeAndConcatenateString(String resultstring, Node childnode)
        {
            if(childnode.getNodeType() != 3)
                throw new IllegalArgumentException("Not a text node");
            String textnodecontents = childnode.getNodeValue();
            textnodecontents = textnodecontents.trim();
            if(resultstring.length() > 0 && textnodecontents.length() > 0)
                resultstring = resultstring + " ";
            return resultstring + textnodecontents;
        }

        public Support()
        {
        }
    }


    public abstract void readAttribute(String s, String s1, Node node);

    public abstract void readChildNode(String s, short word0, Node node);
}